<?php

namespace App\Http\Controllers;

use App\Models\User;
use Exception;
use Illuminate\Http\Request;

/**
 * 账号管理
 */
class AccountController extends Controller
{
    /**
     * 修改昵称
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function modify_nickname(Request $request)
    {
        try {
            $status   = 0;
            $data     = [];
            $userID   = $request->get('user_id');
            $nickname = $request->get('nickname');

            if (empty($userID) || empty($nickname)) {
                throw new Exception("缺少参数！");
            }
            $user = User::where(['userID' => $userID])->first();
            if (!$user) {
                throw new Exception('未找到用户');
            }

            $user->update([
                'nickname' => $nickname,
            ]);

            $status           = 1;
            $msg              = '操作成功';
            $data['user_id']  = $user->userID;
            $data['nickname'] = $user->nickname;
            $data['account']  = $user->account;
            $data['headimg']  = $user->headimg;

        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }
}
